/* Includes ------------------------------------------------------------------*/
#include "SystemTick.h"
#include "Led.h"

/* Private typedef -----------------------------------------------------------*/
typedef void (*output_f)(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef PortPins);

/* Private define ------------------------------------------------------------*/
#define LED_PRESCALE    100

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
bool s_Led_IntFlag = false;
U8   s_Led_PrescaleCnt = 0;

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/* Public functions ----------------------------------------------------------*/

void Led_TimerInterrupt(void)
{
    if (LED_PRESCALE <= ++s_Led_PrescaleCnt)
    {
        s_Led_PrescaleCnt = 0;
        s_Led_IntFlag = true;
    }
}


void Led_Init(void)
{
    // Initialize I/Os in Output Mode
    GPIO_Init(RELAY1_PORT, RELAY1_PINS, GPIO_MODE_OUT_PP_HIGH_SLOW);
    GPIO_Init(RELAY2_PORT, RELAY2_PINS, GPIO_MODE_OUT_PP_HIGH_SLOW);
    GPIO_Init(RELAY3_PORT, RELAY3_PINS, GPIO_MODE_OUT_PP_HIGH_SLOW);
    GPIO_Init(RELAY4_PORT, RELAY4_PINS, GPIO_MODE_OUT_PP_HIGH_SLOW);
    GPIO_Init(RELAY5_PORT, RELAY5_PINS, GPIO_MODE_OUT_PP_HIGH_SLOW);
    GPIO_Init(RELAY6_PORT, RELAY6_PINS, GPIO_MODE_OUT_PP_HIGH_SLOW);

    Led_SetAllOutput( false );

    SysTick_Registration(&Led_TimerInterrupt);
}


void Led_SetAllOutput( bool iOn )
{
    Led_SetOutput( RELAY1, iOn );
    Led_SetOutput( RELAY2, iOn );
    Led_SetOutput( RELAY3, iOn );
    Led_SetOutput( RELAY4, iOn );
    Led_SetOutput( RELAY5, iOn );
    Led_SetOutput( RELAY6, iOn );
}

void Led_SetOutput( eOutput iOutput, bool iOn )
{
    output_f outputCtrl_f = ( true == iOn )? &GPIO_WriteHigh : &GPIO_WriteLow;

    switch ( iOutput )
    {
    case RELAY1:
        {
            outputCtrl_f( RELAY1_PORT, RELAY1_PINS );
        } break;

    case RELAY2:
        {
            outputCtrl_f( RELAY2_PORT, RELAY2_PINS );
        } break;

    case RELAY3:
        {
            outputCtrl_f( RELAY3_PORT, RELAY3_PINS );
        } break;

    case RELAY4:
        {
            outputCtrl_f( RELAY4_PORT, RELAY4_PINS );
        } break;

    case RELAY5:
        {
            outputCtrl_f( RELAY5_PORT, RELAY5_PINS );
        } break;

    case RELAY6:
        {
            outputCtrl_f( RELAY6_PORT, RELAY6_PINS );
        } break;

    default:
        {
        } break;
    } 
}


void Led_InvOutput( eOutput iOutput )
{
    switch ( iOutput )
    {
    case RELAY1:
        {
            Led_SetOutput( iOutput, (bool)(RESET == GPIO_ReadInputPin(RELAY1_PORT, RELAY1_PINS)) );
        } break;

    case RELAY2:
        {
            Led_SetOutput( iOutput, (bool)(RESET == GPIO_ReadInputPin(RELAY2_PORT, RELAY2_PINS)) );
        } break;

    case RELAY3:
        {
            Led_SetOutput( iOutput, (bool)(RESET == GPIO_ReadInputPin(RELAY3_PORT, RELAY3_PINS)) );
        } break;

    case RELAY4:
        {
            Led_SetOutput( iOutput, (bool)(RESET == GPIO_ReadInputPin(RELAY4_PORT, RELAY4_PINS)) );
        } break;

    case RELAY5:
        {
            Led_SetOutput( iOutput, (bool)(RESET == GPIO_ReadInputPin(RELAY5_PORT, RELAY5_PINS)) );
        } break;

    case RELAY6:
        {
            Led_SetOutput( iOutput, (bool)(RESET == GPIO_ReadInputPin(RELAY6_PORT, RELAY6_PINS)) );
        } break;

    default:
        {
        } break;
    } 
}


void Led_RunOne(void)
{
//     static U8 s_Cnt = 0;
//     if (true == s_Led_IntFlag)
//     {
//         s_Led_IntFlag = false;
//         
//         if (5 <= ++s_Cnt)
//         {
//             s_Cnt = 0;
//             Led_InvOutput( SLEEP_LED2 );
//         }
//     }
}
